package com.example.boot.handle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.boot.ResultEnum.ResultEnum;
import com.example.boot.exception.BootException;
import com.example.boot.util.ResultData;
import com.example.boot.util.ResultUtil;

/**
 * 捕获异常为了错误时候而返回的
 * 
 * @author ydx
 *
 */
@ControllerAdvice
public class ExceptionHandle {

	private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

	/**
	 * 只要是异常抛出之后，就交给@ExceptionHandler来处理
	 * 
	 * @return
	 */
	@ExceptionHandler(value = Exception.class)
	@ResponseBody
	public ResultData handle(Exception e) {
		if (e instanceof BootException) {
			BootException exception = (BootException) e;
			return ResultUtil.error(exception.getCode(), exception.getMessage());

		}

		logger.info("【系统异常】", e);
		return ResultUtil.error(ResultEnum.SYSTEM_EXC.getCode(), ResultEnum.SYSTEM_EXC.getMsg());
		// 如果是特定抛出异常，则以下
		// return ResultUtil.error(100, e.getMessage());

	}

}
